/**
 * 系统名称: 企业财资
 * 模块名称: repair-boot
 * 类 名 称: scheduledJob.java
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改人员:
 * 修改说明:
 * ============ ============================================
 * zhanghong 创建
 * ============ ============================================
 */
package com.mt.common.system.entity.scheduledJob;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mt.common.core.utils.SpringContextUtils;
import org.quartz.*;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 *
 * @author zhanghong
 * @date 2023-03-17 15:52
 */
public class ScheduledJob implements Job {
    private static final long serialVersionUID = 1L;
    @Override
    @Transactional(rollbackFor = {Throwable.class})
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //System.out.println("定时器执行:START>>>>>>>>>>>>>>>>>");
        //获取对应的bean
        JobDataMap jobDataMap = jobExecutionContext.getTrigger().getJobDataMap();
        Object bean = SpringContextUtils.getBean((String) jobDataMap.get(JobConst.BEANNAME));
        //获取方法
        Method method = null;
        String methodName=(String) jobDataMap.get(JobConst.METHODNAME);
        Object methodParams = jobDataMap.get(JobConst.METHODPARAMS);
        try {
            if (methodParams != null) {
                method = bean.getClass().getDeclaredMethod(methodName,String.class);
                method.invoke(bean,methodParams);
            } else {
                method=bean.getClass().getDeclaredMethod(methodName);
                method.invoke(bean);
            }
        }catch(NoSuchMethodException e){
            e.printStackTrace();
            throw new JobExecutionException(e.toString());
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            throw new JobExecutionException(e.toString());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new JobExecutionException(e.toString());
         }
        //执行完毕
        //System.out.println("定时器执行:OVER>>>>>>>>>>>>>>>>>>");
    }
}
